package com.zmzncs.lmtc.module.customer.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zmzncs.lmtc.common.pojo.PageInfo;
import com.zmzncs.lmtc.common.pojo.Rest;
import com.zmzncs.lmtc.common.util.*;
import com.zmzncs.lmtc.module.customer.pojo.card.CardDTO;
import com.zmzncs.lmtc.module.customer.pojo.customer.*;
import com.zmzncs.lmtc.module.customer.service.CustomerService;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 客户-客户信息 前端控制器
 * </p>
 *
 * @author 任建波
 * @since 2020-08-26
 */
@Api(tags = "客户")
@RestController
@RequestMapping("/customer/info")
@Validated
public class CustomerController {

    @Autowired
    CustomerService customerService;

    @ApiOperation(value = "查询会员列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true)
    })
    @GetMapping(value = "/getCustomerPage")
    public Rest<PageInfo<CustomerTableVO>> getCustomerPage(@Valid CustomerSearchForm searchForm) {
        IPage<CustomerDTO> customerDTOPage = customerService.getCustomerPage(searchForm);

        PageInfo<CustomerTableVO> customerTableVOPage = JsonUtil.pageTransform(customerDTOPage, CustomerTableVO.class);

        return RestUtil.success(customerTableVOPage);
    }

    @ApiOperation(value = "获取正式会员和潜在会员数量")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true)
    })
    @GetMapping(value = "/getVipCount")
    public Rest<CustomerVipVO> getVipCount(){
        CustomerVipVO vipCount = customerService.getVipCount();
        return RestUtil.success(vipCount);
    }

    @ApiOperation(value = "会员详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType="query", name = "id", value = "客户id", required = true),
    })
    @PostMapping(value = "/getCustomerInfo")
    public Rest<CustomerInfoVO> getCustomerInfo(@NotNull Long id) {
        CustomerDTO customerDTO = customerService.getCustomerInfo(id);
        CustomerInfoVO customerInfoVO = new CustomerInfoVO();
        BeanUtils.copyProperties(customerDTO, customerInfoVO);
        return RestUtil.success(customerInfoVO);
    }

    @ApiOperation(value = "查询会员账户")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @PostMapping(value = "/getCustomerAccount")
    public Rest<CustomerAccountVO> getCustomerAccount(@Valid CustomerAccountForm customerAccountForm) {
        List<CardDTO> cardDTOList = customerService.getCustomerAccount(customerAccountForm);
        List<CustomerAccountVO> customerAccountVOList = JsonUtil.listTransform(cardDTOList, CustomerAccountVO.class);
        return RestUtil.success(customerAccountVOList);
    }


    @ApiOperation(value = "手机号/昵称查询会员信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType = "query", name = "info", value = "手机号/昵称"),
    })
    @GetMapping(value = "getCustInfoByPhoneOrName")
    public Rest<List<CustomerGrantTicketVO>> getCustInfoByPhoneOrName(@NotNull String info) {
        List<CustomerDTO> custInfoDTOS = customerService.getCustInfoByPhoneOrName(info);
        List<CustomerGrantTicketVO> infoList = JsonUtil.listTransform(custInfoDTOS, CustomerGrantTicketVO.class);
        return RestUtil.success(infoList);
    }

    @ApiOperation(value = "会员账户详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType="query", name = "customerId", value = "客户id", required = true),
    })
    @PostMapping(value = "/getAccountDetails")
    public Rest<Map<String,Object>> getAccountDetails(@NotNull Long customerId) {
        Map<String,Object> map = customerService.getAccountDetails(customerId);
        return RestUtil.success(map);
    }


    @ApiOperation(value = "向用户转账")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType="query", name = "orderCode", value = "订单编号", required = true),
            @ApiImplicitParam(paramType="query", name = "openid", value = "用户openid", required = true),
            @ApiImplicitParam(paramType="query", name = "totalFee", value = "转账总金额", required = true),
            @ApiImplicitParam(paramType="query", name = "desc", value = "转账说明", required = true),
    })
    @GetMapping(value = "/transfers-to-user")
    public Rest<String> transfersToUser(String orderCode,String openid, BigDecimal totalFee, String desc){
        String str = customerService.transfersToUser(orderCode,openid,totalFee,desc);
        return RestUtil.success(str);
    }



}

